package com.wsj.service.system;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.wsj.util.exception.BusinessException;

public class ApplicationUtilImpl implements IApplicationUtil {
	

	private static final  String  URL_PATTERN = "qqddc.com";
	
	
	/*****
	 * 获取请求前面的名称
	 */
	public String getDealerNameFromURL(HttpServletRequest request)throws BusinessException 
	{
		String dealerName="";
		String servername = request.getServerName();
		String patternStr = URL_PATTERN;
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(servername);
		if(matcher.find())
		{
			dealerName = servername.substring(0,matcher.start());
		}
		
		return dealerName;
		
	}
	/**
	 * 根据二级域名得到经销商ID
	 * @return int 返回 -1 则表示没有找到对应的经销商。 -1 代表的是 qqddc.com 和 www.qqddc.com 返回的值
	 * @param HttpServletRequest request 
	 */

	public int getDealerIdFromURL(HttpServletRequest request)
			throws BusinessException {
		String servername = request.getServerName();
		String patternStr = URL_PATTERN;
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(servername);
        if(matcher.find()){
			String scd = servername.substring(0,matcher.start());	
			try{
				
				if("sale.".equals(scd) || "".equals(scd)){
					return -1;
				}else{
					String ops = scd.substring(0,scd.lastIndexOf("."));
					if(ops.indexOf(".")==-1){
						int id = 0;
						if(id==0)return -1;
						else return id;
					}else{
						String ops2 = ops.substring(0,ops.lastIndexOf("."));
						int id = 0;
						if(id==0)return -1;
						else return id;
					}	
				}	
			}catch(Exception e){
				throw new BusinessException("域名解析错误");
			}
		}
		return -1;
	}
	
	
	
	
	

}
